/*  
 *  Copyright 2006 Paul Jack.
 *
 *  This file is part of the Dex compiler suite. 
 *  
 *  Dex is free software; you can redistribute it and/or modify it
 *  under the terms of the GNU General Public License as published by the
 *  Free Software Foundation; either version 2 of the License, or (at your
 *  option) any later version.
 *  
 *  Dex is distributed in the hope that it will be useful, but
 *  WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
 *  Public License for more details.
 *  
 *  You should have received a copy of the GNU General Public License along
 *  with this program; if not, write to the Free Software Foundation, Inc.,
 *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
 */
package dex.compiler.interpreter.value;


import dex.compiler.checker.type.IntegerType;
import dex.compiler.checker.type.Type;


/**
 * An integer value.
 */
public class IntegerValue extends Value {
	

	/**
	 * The value.
	 */
	final private long value;


	/**
	 * The width in bits of the value.
	 */
	final private int width;


	/**
	 * True if the value is signed, false otherwise.
	 */
	final private boolean signed;


	/**
	 * Constructs a new integer value.
	 * 
	 * @param signed  true if the value is signed, false otherwise
	 * @param width   the width in bits of the value
	 * @param v       the actual numeric value
	 */
	public IntegerValue(boolean signed, int width, long v) {
		int shift = 64 - width;
		if (signed) {
			this.value = (v << shift) >> shift;
		} else {
			this.value = (v << shift) >>> shift;
		}
		this.width = width;
		this.signed = signed;
	}


	/**
	 * Returns the actual numeric value.
	 * 
	 * @return  the actual numeric value
	 */
	public long value() {
		return value;
	}


	/**
	 * Returns the width in bits of the value.
	 * 
	 * @return  the width in bits
	 */
	public int width() {
		return width;
	}


	/**
	 * Returns true if the value is signed.
	 * 
	 * @return  the signage
	 */
	public boolean signed() {
		return signed;
	}


	@Override
	public String toString() {
		return Long.toString(value);
	}


	@Override
	public Type type() {
		return IntegerType.get(signed, width);
	}

}
